package com.aldobucchi.graph
{
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	
	import com.aldobucchi.graph.query.Query;
	import com.aldobucchi.graph.query.QueryEvent;


	/**
	 * 
	 * TODO: add paging et al
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class QueryCollectionView extends ArrayCollection
	{
		
		
		
		
		private var query:Query;
		
		
		public function QueryCollectionView( query:Query )
		{

			this.query = query;

			query.addEventListener( QueryEvent.RESULT, query_result );
			query.execute( );	
			
		}


		
		private function query_result( event:QueryEvent ):void
		{
			
			disableAutoUpdate();
		 	try {
		 	
		 		for each ( var node:* in event.target.result ) {
					addItem( node );		 		
		 		}
		 	
		 	} catch ( e:Error ){}
			enableAutoUpdate();	
		}
		
		
		
		public static function forQuery( query:Query ):ICollectionView
		{
			return new QueryCollectionView( query );
		}
		
		
	}

}